/**
 * @file    WebOleClientSite.cpp
 * @brief   
 * @date    2005-03-15 15:55:23
 * @author  GengYong (gengyong@gmail.com)
 *
 */

#include <cassert>
#include "WebOleClientSite.h"

using namespace xessage_private;

WebOleClientSite::WebOleClientSite(WebWindowImpl * app):m_appImpl(app), m_refCount(1)
{
	m_inPlaceSite = new WebOleInPlaceSite(app);
	m_hostUIHandler = new WebDocHostUIHandler(app);
	m_hostShowUI = new WebDocHostShowUI(app);
	return;
}


WebOleClientSite::~WebOleClientSite()
{
	if (m_hostShowUI)
	{
		m_hostShowUI->Release();
		m_hostShowUI = NULL;
	}
	
	if (m_hostUIHandler)
	{
		m_hostUIHandler->Release();
		m_hostUIHandler = NULL;
	}

	if (m_inPlaceSite)
	{
		m_inPlaceSite->Release();
		m_inPlaceSite = NULL;
	}
	return;
}


HRESULT STDMETHODCALLTYPE WebOleClientSite::QueryInterface(REFIID riid, LPVOID * ppvObject)
{
	if (ppvObject == NULL) return E_POINTER;

	if ((riid == IID_IUnknown) || (riid == IID_IOleClientSite))
	{
		this->AddRef();
		*ppvObject = this;
	}
	else if (riid == IID_IOleInPlaceSite)
	{
		return this->m_inPlaceSite->QueryInterface(riid, ppvObject); 
	}
	else if (riid == IID_IDocHostUIHandler)
	{
		return this->m_hostUIHandler->QueryInterface(riid, ppvObject); 
	}
	else if (riid == IID_IDocHostShowUI)
	{
		return this->m_hostShowUI->QueryInterface(riid, ppvObject);
	}
	else 
	{
		*ppvObject = NULL;
		return E_NOINTERFACE;
	}
	return S_OK;
}


ULONG STDMETHODCALLTYPE WebOleClientSite::AddRef()
{
	return InterlockedIncrement(&m_refCount);
}


ULONG STDMETHODCALLTYPE WebOleClientSite::Release()
{
	LONG result = InterlockedDecrement(&m_refCount);
	assert(result >= 0);
	if (result == 0)
	{
		delete this;
	}
	return (ULONG)(result);
}


HRESULT STDMETHODCALLTYPE WebOleClientSite::SaveObject()
{
	return E_NOTIMPL;;
}


HRESULT STDMETHODCALLTYPE WebOleClientSite::GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, IMoniker ** ppmk)
{
	return E_NOTIMPL;;
}


HRESULT STDMETHODCALLTYPE WebOleClientSite::GetContainer(LPOLECONTAINER * ppContainer)
{
	if (ppContainer != NULL) *ppContainer = NULL;
	return E_NOINTERFACE;
}


HRESULT STDMETHODCALLTYPE WebOleClientSite::ShowObject()
{
	return S_OK;
}


HRESULT STDMETHODCALLTYPE WebOleClientSite::OnShowWindow(BOOL fShow)
{
	return E_NOTIMPL;;
}


HRESULT STDMETHODCALLTYPE WebOleClientSite::RequestNewObjectLayout()
{
	return E_NOTIMPL;;
}



